<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>近期活跃板块</title>
		<link href="css/text.css" rel="stylesheet" type="text/css" />
		<style type="text/css">							
			.fixedBox{					
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 8px;     
				height: 8px;				
			}			
		</style>		
		<script type="text/javascript" src="js/json2html.js"></script>
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script>
			window.onload = function() {
			getSource();
			changeStyle();
			}
						
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
					getSource();					
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {
				time_range("9:00","15:15")						
				},
				60000);
			})
			
			function changeStyle() {
				var obj = document.getElementById("towrite");	
				obj.style.left= "0px"; 				
				obj.style.width= "520px";
				obj.style.border="none";		
				
				var obj1 = document.getElementById("towrite1");									
				obj1.style.width= "260px";	//左侧宽度
				obj1.style.fontSize="12px";	//左侧字体		
				
				var obj2 = document.getElementById("towrite2");
				obj2.style.left= "250px";   //右侧左边位置
				obj2.style.width= "250px";  //右侧宽度
				obj2.style.fontSize="12px";	//右侧字体	 		  		  		  			  
			}		
			
			function createXmlHttp() {
				if (window.XMLHttpRequest) {
				xmlHttp1 = new XMLHttpRequest();
				xmlHttp2 = new XMLHttpRequest();
				//xmlHttp3 = new XMLHttpRequest();
				} else {
				xmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP");
				xmlHttp2 = new ActiveXObject("Microsoft.XMLHTTP");
				//xmlHttp3 = new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
			
			function changeCss(obj,id){				
				var alist = document.getElementById(id).getElementsByTagName("a");					
				for(var i = 0; i < alist.length; i++){				
					if(alist[i] == undefined){
						break;
					}
					alist[i].style.color = "#d2d2d3"; //给所有a标签赋原色
				}
				obj.style.color = "#00FFFF"; //令当前标签高亮				
			}
			
			function getSource() {	    							
				var url1 = "https://apphq.longhuvip.com/w1/api/index.php?&c=Index&a=GetInfo&View=2"; 
				createXmlHttp();				
				xmlHttp1.onreadystatechange = writeSource;
				xmlHttp1.open("GET", url1, true);
				xmlHttp1.send(null);				
			}
			
			function UnUnicode(str) {
				return unescape(str.replace(/\\/g, "%"));
			}
			

			function writeSource() { 
				if (xmlHttp1.readyState == 4) { 
					div1 = document.getElementById('towrite1');
					div1.innerHTML = "<h2>近期活跃板块</h2>";				
					var data1 = xmlHttp1.responseText	
					
					data1 = data1.replace(/ /g,"").match(/(\S*)\]\],\"/)[1] + ']]';	
					data1 = data1.replace(/(\[\").*?(?=\])/g,function(word){ str = word.match(/\[(\S*)/)[1]; return '{"code":' + str.split(',')[2] + ',"name":' + str.split(',')[0]+',"zf":'+str.split(',')[1];});
					data1 = data1.replace(/\],\{/g,"},{").replace(/\]\]/g,"}]}");	
					//document.write(data1);
					ss = data1.split('},{');				
					for (i = 0; i < ss.length; i++)
					{
						var code = ss[i].match(/\"code\":(\S*),\"name/)[1];						
						var name = ss[i].match(/\"name\":\"(\S*)\",\"zf/)[1];						
						name = UnUnicode(name);												
						var zf = ss[i].match(/\"zf\":\"(\S*)\"/)[1].replace("}]}","");				
						var bks = "<a href='javascript:void(0)' onclick='update(" + "\"" +
									code + "\"+\"#\"+" + "\"" + name + "\"" + ")'>" + (i + 1) + "、" +
									code + "&nbsp;&nbsp;" + name + 
									"&nbsp;&nbsp;涨幅：" + zf + "%" + "</a>"	;
						
						var bkdm = code;
						subclass(bkdm , bks);
						if (i == 0)
						{
							temp = code + ";";
							update(code + "#" + name);
						}
					}
				}				
			}
			
			//子类
			function subclass(bkdm , bks) {
				var url3 = "https://apphq.longhuvip.com/w1/api/index.php?a=SonPlate_Info&c=ZhiShuRanking&apiv=w26&PlateID=" + bkdm + "&"; 
				var xhr = new XMLHttpRequest();	
				xhr.open("GET", url3, false);//同步 
				xhr.send(null)																		
				div1 = document.getElementById('towrite1');
				//div1.innerHTML = div1.innerHTML + "<ul><li>" + bks + "</li></ul>";
				var data3 = xhr.responseText;						
				if(data3.indexOf('\"List\":[]') == -1) {
					var dms = bks.split('、');					
					div1.innerHTML = div1.innerHTML + "<p>" + dms[0] + "、★" + dms[1] + "</p>";					
					
					//添加分类Div
					var newDiv = document.createElement("div");
					newDiv.id = bkdm;
					newDiv.style.display = "none";
					document.getElementById("towrite1").appendChild(newDiv);
										
					//data3 = data3.replace(/ /g,"").match(/(\S*)\]\],\"/)[1] + ']]';	
					data3 = data3.replace(/ /g,"").match(/\{\"List\":(\S*)\]\],\"/)[1] + ']]';	
					data3 = data3.replace(/(\[\").*?(?=\])/g,function(word){ str = word.match(/\[(\S*)/)[1]; return '{"code":' + str.split(',')[0] + ',"name":' + str.split(',')[1]+',"qd":'+str.split(',')[2];});							
					data3 = data3.replace(/\],\{/g,"},{").replace(/\]\]/g,"}]");
					//alert(data3);	
					//document.write(data3);

					var result = JSON.parse(data3); //JSON字符串转换为对象
					function up(a,b){
					return b.qd-a.qd
					}
					result.sort(up);
					result = JSON.stringify( result );						
					data3= '{\"List\":' + result + "}";
					//document.write(data3);

					var transform_ee1 = [           
					{"tag":"ul", children:function(){return(json2html.transform(this.List, transform_ff1));}}
					];

					var transform_ff1= 
					{"tag":"p", children:[
					{"tag":"p", children:[
					{"tag": "li", "html":"<a href='javascript:void(0)' onclick='update(\"${code}\"+\"#\"+\"${name}\")'>${code} ${name}&nbsp;&nbsp;强度：${qd}</a>"}
					]}]};

					var html3 = json2html.transform(data3, transform_ee1);		
					//div1 = document.getElementById('towrite1');
					div1 = document.getElementById(bkdm);
					div1.innerHTML = div1.innerHTML + html3;				
				}
				else
				{
					div1.innerHTML = div1.innerHTML + "<p>" + bks + "</p>";
				}					
			}
			
			var temp = "";				
			function update(ss) {
				//alert(ss);
				var dms=ss.split('#');
				dm = dms[0];
				bk = dms[1];
				//alert(temp);
				if(document.getElementById(dm))
				{					
					div1 = document.getElementById(dm);
					if(temp.indexOf(dm) == -1)
					{
						temp += dm + ";";
						div1.style.display = "block";
					}
					else
					{
						temp = temp.replace(dm + ";" , "");
						div1.style.display = "none";
					}
				}
				//alert(temp);
				div2 = document.getElementById('towrite2');				
				div2.innerHTML = '<h4>' + bk + '</h4>';				
				var url2 = "https://apphq.longhuvip.com/w1/api/index.php?Order=1&st=30&a=ZhiShuStockList_W8&c=ZhiShuRanking&PhoneOSNew=1&old=1&apiv=w21&Type=6&PlateID=" + dm + "&"; 								
				createXmlHttp();
				xmlHttp2.onreadystatechange = writeSource2;				
				xmlHttp2.open("GET", url2, true);
				xmlHttp2.send(null);				
			}
			
			function writeSource2() { 			
				if (xmlHttp2.readyState == 4) {                  	
				var data2 = xmlHttp2.responseText
				
				//data2 = data2.replace(/ /g,"").match(/(\S*)\]\],\"/)[1] + ']]';	
				data2 = data2.replace(/ /g,"").match(/{"list":(\S*)/)[1].split("]]")[0] + ']]';	
				//data2 = data2.replace(/(\[\").*?(?=\])/g,function(word){ str = word.match(/\[(\S*)/)[1]; return '{"code":' + str.split(',')[0] + ',"name":' + str.split(',')[1]+',"zf":'+str.split(',')[6];});
				data2 = data2.replace(/(\[\").*?(?=\])/g,function(word){ str = word.match(/\[(\S*)/)[1]; return '{"code":' + str.split(',')[0] + ',"name":' + str.split(',')[1]+',"zf":'+str.split(',')[6] +',"lz":'+str.split(',')[24] +',"zl":'+str.split(',')[2] +',"lb":'+str.split(',')[23];});
				data2 = '{\"list\":' + data2.replace(/\],\{/g,"},{").replace(/\]\]/g,"}]") + "}";
				//data2 = data2.replace(/\],\{/g,"},{").replace(/\]\]/g,"}]");					
				//document.write(data2);
				
				<!-- var result = JSON.parse(data2); //JSON字符串转换为对象 -->
				<!-- function up(a,b){ -->
					<!-- return b.zf-a.zf -->
				<!-- } -->
				<!-- result.sort(up); -->
				<!-- result = JSON.stringify( result );						 -->
				<!-- data2= '{\"list\":' + result + "}"; -->
				//document.write(data2);
				
				var transform_aa2 = [           
				{"tag":"p", children:function(){return(json2html.transform(this.list, transform_bb2));}}
				];
				
				var transform_bb2= 
				{"tag":"p", children:[
				{"tag":"p", children:[							
				//{"tag": "li", "html":"<a href='http://www.treeid/code_${code}' >${code} ${name}&nbsp;&nbsp;涨幅：${zf}%</a>"}, 							
				{"tag": "p", "html":"<a href='http://www.treeid/code_${code}' onclick='changeCss(this,\"towrite2\")'>${code} ${name}&nbsp;&nbsp;涨幅：${zf}%&nbsp;&nbsp;${lz}</a>"}, 
				]}]}; 
				
				
				var html2 = json2html.transform(data2, transform_aa2);		
				div2 = document.getElementById('towrite2');
				div2.innerHTML = div2.innerHTML + html2;
				//div2.innerHTML = '<h2>板块个股</h2>' + html2;				
				}
			}
		</script>
	</head>
	<body>		
		<div id="towrite" class="fixedBox">
			<div id="towrite1" ></div>		
			<div id="towrite2" class="fixedBox" ></div>
		</div>
	</body>
</html>